package com.tenbit.zip.svc;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.tenbit.com.vo.ErrorMsgVO;
import com.tenbit.zip.dao.CodeBookEDAO;
import com.tenbit.zip.vo.CodeBookVO;

/**
 * 코드집을 관리하기 위한 SVCImpl<br>
 * <PRE>
 * SystemName: 텐비트 4주차 workshop
 * BusinessName: 코드집(ZIP)
 * Author: 강현구
 * Class Name(en): ZIP1000SVCImpl.java
 * Class Name(kor): 코드집 관리 SVCImpl
 * Descripttion: 코드집을 관리하기 위한 SVCImpl
 * Modification history
 * 수정일      		수정자     		수정내용
 * -------  	---------	------------
 * 2014.04.28	강현구	      최초 생성   
 * </PRE>
 *
 */
@Service
public class ZIP1000SVCImpl implements ZIP1000SVC{
	@Autowired
	private CodeBookEDAO codeBookEDAO;
	private Logger log = Logger.getLogger(this.getClass());
	
	public CodeBookVO mapToCodeBookVO(Map<String, Object> map){
		
		CodeBookVO codeBookVO = new CodeBookVO();
		
		codeBookVO.setCodeType((String)map.get("codeType"));
		codeBookVO.setCodeDetail((String)map.get("codeDetail"));
		codeBookVO.setCodeName(((String)map.get("codeName")));
		
		codeBookVO.setSortOrder((Integer)map.get("sortOrder"));
			
		return codeBookVO;
	}
	
	@Override
	public List<CodeBookVO> listCodeBookType(Map<String, Object> paraMap) {
		return codeBookEDAO.listCodeBookType(paraMap);
	}

	@Override
	public List<CodeBookVO> listCodeBookDetail(Map<String, Object> paraMap) {
		return codeBookEDAO.listCodeBookDetail(paraMap);
	}
	
	@Override
	public ErrorMsgVO exeCodeBook(List<Map<String, Object>> dataSet) {
		
		log.debug("=======start: "+this.getClass().getName()+"."+new Exception().getStackTrace()[0].getMethodName()
				+"=======");
		ErrorMsgVO errMsg = new ErrorMsgVO();
		
		CodeBookVO codeBookVO;
		
		for (Map<String, Object> map : dataSet) {
			if(map.get("CRUD").equals("C")){
				//dup처리
				
				codeBookVO = mapToCodeBookVO(map);
				
				try {
					codeBookEDAO.insertCodeBook(codeBookVO);
					
				} catch (Exception e) {
					if(e instanceof DataIntegrityViolationException){
						
						if( codeBookVO.getCodeType().equals(codeBookVO.getCodeDetail()) ){
							errMsg.setErrMsg("중복된 분류코드가 존재합니다: "+codeBookVO.getCodeType());
							errMsg.setErrExist(true);
							
						}else{
							errMsg.setErrMsg("중복된 상세코드가 존재합니다: "+codeBookVO.getCodeDetail());
							errMsg.setErrExist(true);

						}
						
						TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
						
					}

				}
				
			}else if(map.get("CRUD").equals("U")){
				
				codeBookVO = mapToCodeBookVO(map);
				
				codeBookEDAO.updateCodeBook(codeBookVO);
				
			}else if(map.get("CRUD").equals("D")){
				
				codeBookVO = mapToCodeBookVO(map);
				
				codeBookEDAO.deleteCodeBook(codeBookVO);
				
			}
		}
		
		log.debug("=======end: "+this.getClass().getName()+"."+new Exception().getStackTrace()[0].getMethodName()
				+"=======");
		
		return errMsg;
	}

	@Override
	public ErrorMsgVO doValidateDetail(List<Map<String, Object>> list) {
		
		ErrorMsgVO errMsg = new ErrorMsgVO();
		
		String codeType = (String)list.get(0).get("codeType");
		
		if( codeBookEDAO.selectCodeBookExist(codeType) == 0 ){
			errMsg.setErrMsg("분류코드가 존재하지 않습니다.");
			errMsg.setErrExist(true);
			
			return errMsg;
		}
		
		for (Map<String, Object> map : list) {
			if( ((String)map.get("codeDetail")).equals("") ){
				errMsg.setErrMsg("상세코드를 입력해주세요");
				errMsg.setErrExist(true);
				
				return errMsg;
			}
			
			if( ((String)map.get("codeName")).equals("") ){
				errMsg.setErrMsg("상세코드명을 입력해주세요");
				errMsg.setErrExist(true);
				
				return errMsg;
			}
			
			if( ((Integer)map.get("sortOrder")) <= 0 ){
				errMsg.setErrMsg("1이상 입력해 주세요");
				errMsg.setErrExist(true);
				
				return errMsg;
			}
		}
		
		return errMsg;
	}

	@Override
	public ErrorMsgVO doValidateType(List<Map<String, Object>> list) {
		
		ErrorMsgVO errMsg = new ErrorMsgVO();
		
		for (Map<String, Object> map : list) {
			if( ((String)map.get("codeType")).equals("") ){
				errMsg.setErrMsg("분류코드를 입력해주세요");
				errMsg.setErrExist(true);
				
				return errMsg;
			}
			
			if( ((String)map.get("codeName")).equals("") ){
				errMsg.setErrMsg("분류코드명을 입력해주세요");
				errMsg.setErrExist(true);
				
				return errMsg;
			}
			
		}
		
		return errMsg;
	}

	
	
	
}
